/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VMAddManyWords;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import GuiTools.*;
import Settings.*;
import Storage.*;
import VocabularyManagement.EWDFormularPanel;
import VocabularyManagement.MeaningFormularPanel;

/**
 * 
 * @author Christoph Schnack
 *
 */
class WordRowAdvanced extends JPanel{
	TribuitTextField stNewLectTF,newLectTF;
	JComboBox lectionsCB,termclassACB,termclassBCB;
	JRadioButton toLectRB,newLectRB;
	JCheckBox stLectCB;
	int number;
	GridBagConstraints gbc = new GridBagConstraints();
	JPanel panel,lectSelectionPanel=new JPanel(),errorPanelA,errorPanelB,errorContainerA,errorContainerB,errorContainerAB;
	JLabel errorLabelA,errorLabelB,errorLabelAB;
	JTabbedPane meaningsPanelA,meaningsPanelB;
	boolean init=true;
	Vector<MeaningFormularPanel> meaningsA = new Vector<MeaningFormularPanel>(),meaningsB = new Vector<MeaningFormularPanel>();
	MiniButton delMeaningBtnA,delMeaningBtnB;
	SectionChooser sectionChooser;
	Language LANG;
	Settings SET;
	Vocabulary vocToEdit;
	TribuitFrame parentFrame;
	AddManyWords amwPanel;
	
	public WordRowAdvanced(TribuitFrame parentFrameArg,AddManyWords amwArg, Settings setArg, Vocabulary vte,int nr){
		super();
		parentFrame = parentFrameArg;
		SET = setArg;
		LANG = SET.getLanguage();
		amwPanel = amwArg;
		vocToEdit = vte;
		panel=this;
		panel.setBackground(Color.lightGray);
		panel.setLayout(new GridBagLayout());
		number = nr;
		stLectCB = new JCheckBox(LANG.getDefault(),true);
		stLectCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				sectionChooser.setEnabledTotally(!stLectCB.isSelected());
			}
		});
		stLectCB.setOpaque(false);
		
		
		sectionChooser = new SectionChooser(SET,vocToEdit.getLections());
		sectionChooser.setEnabledTotally(false);
		
		termclassACB = new JComboBox(vocToEdit.getSortedTermClassKeysA());
		termclassACB.setPreferredSize(new Dimension(300,26));
		termclassACB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setPanelsA();
				parentFrame.packAndKeepBounds();
			}
		});
		termclassBCB = new JComboBox(vocToEdit.getSortedTermClassKeysB());
		termclassBCB.setPreferredSize(new Dimension(300,26));
		termclassBCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setPanelsB();
				parentFrame.packAndKeepBounds();
			}
		});
		
		meaningsPanelA = new JTabbedPane();
		meaningsA.add(new EWDFormularPanel(parentFrame,SET,vocToEdit.getTermclassA((String)termclassACB.getSelectedItem()),vocToEdit.getCategoriesA(),1));
		//meaningsA.add(new MeaningRow(parentFrame, SET, vocToEdit.getTermclassA((String)termclassACB.getSelectedItem()), vocToEdit.getCategoriesA()));
		setTabbedPane(meaningsPanelA,meaningsA);
		meaningsPanelA.setOpaque(false);
		
		meaningsPanelB = new JTabbedPane();
		meaningsB.add(new EWDFormularPanel(parentFrame,SET,vocToEdit.getTermclassB((String)termclassBCB.getSelectedItem()),vocToEdit.getCategoriesB(),1));
		//meaningsB.add(new MeaningRow(parentFrame, SET, vocToEdit.getTermclassB((String)termclassBCB.getSelectedItem()), vocToEdit.getCategoriesB()));
		setTabbedPane(meaningsPanelB,meaningsB);
		meaningsPanelB.setOpaque(false);
		
		Font titleFont = new Font(Font.SANS_SERIF, Font.BOLD, 18);
		JLabel langALabel = new JLabel(vocToEdit.getNameA());
		langALabel.setFont(titleFont);
		JLabel langBLabel = new JLabel(vocToEdit.getNameB());
		langBLabel.setFont(titleFont);
		
		LabelPanel t1LP = new LabelPanel(LANG.getTermClass()+":",termclassACB);
		t1LP.setOpaque(false);
		LabelPanel t2LP = new LabelPanel(LANG.getTermClass()+":",termclassBCB);
		t2LP.setOpaque(false);
		
		JPanel sectionPanel = new JPanel();
		LabelPanel stLectLP = new LabelPanel(LANG.getLection()+":",stLectCB);
		stLectLP.setOpaque(false);
		sectionPanel.add(stLectLP);
		sectionPanel.add(sectionChooser);
		
		JPanel editBtnPanelA = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editBtnPanelA.setOpaque(false);
		MiniButton addMeaningBtnA = new MiniButton(GuiTools.MINI_PLUS_ICON,GuiTools.MINI_PLUS_HOVERED_ICON,GuiTools.MINI_PLUS_DISABLED_ICON,true,new AddMeaningListenerA());
		delMeaningBtnA = new MiniButton(GuiTools.MINI_MINUS_ICON,GuiTools.MINI_MINUS_HOVERED_ICON,GuiTools.MINI_MINUS_DISABLED_ICON,true,new DelMeaningListenerA());
		JLabel eBtnLabelA = new JLabel(LANG.getMeaning()+":");
		editBtnPanelA.add(eBtnLabelA);
		editBtnPanelA.add(addMeaningBtnA);
		editBtnPanelA.add(delMeaningBtnA);
		editBtnPanelA.setOpaque(false);
		
		JPanel editBtnPanelB = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editBtnPanelB.setOpaque(false);
		MiniButton addMeaningBtnB = new MiniButton(GuiTools.MINI_PLUS_ICON,GuiTools.MINI_PLUS_HOVERED_ICON,GuiTools.MINI_PLUS_DISABLED_ICON,true,new AddMeaningListenerB());
		delMeaningBtnB = new MiniButton(GuiTools.MINI_MINUS_ICON,GuiTools.MINI_MINUS_HOVERED_ICON,GuiTools.MINI_MINUS_DISABLED_ICON,true,new DelMeaningListenerB());
		JLabel eBtnLabelB = new JLabel(LANG.getMeaning()+":");
		editBtnPanelB.add(eBtnLabelB);
		editBtnPanelB.add(addMeaningBtnB);
		editBtnPanelB.add(delMeaningBtnB);
		
		errorLabelA = new JLabel();
		errorLabelA.setForeground(Color.red);
		errorPanelA = new JPanel(new FlowLayout(FlowLayout.LEFT));
		errorPanelA.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.red),LANG.getInvalidInput()));
		errorPanelA.add(errorLabelA);
		errorContainerA = new JPanel(new FlowLayout(FlowLayout.LEFT));
		errorContainerA.setOpaque(false);
		
		errorLabelB = new JLabel();
		errorLabelB.setForeground(Color.red);
		errorPanelB = new JPanel(new FlowLayout(FlowLayout.LEFT));
		errorPanelB.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.red),LANG.getInvalidInput()));
		errorPanelB.add(errorLabelB);
		errorContainerB = new JPanel(new FlowLayout(FlowLayout.LEFT));
		errorContainerB.setOpaque(false);
		
		errorLabelAB = new JLabel();
		errorLabelAB.setForeground(Color.red);
		errorContainerAB = new JPanel(new FlowLayout(FlowLayout.LEFT));
		errorContainerAB.setOpaque(false);
		
		JLabel nrLabel = new JLabel(number+".:");
		nrLabel.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
		
		JPanel nrPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		nrPanel.setOpaque(false);
		nrPanel.add(nrLabel);
		nrPanel.add(errorContainerAB);
		
		gbc.insets=new Insets(10,10,10,10);
		gbc.anchor=GridBagConstraints.NORTHWEST;
		gbc.gridwidth=2;
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.weighty=1;
		panel.add(nrPanel,gbc);
		
		gbc.insets=new Insets(0,5,0,5);
		gbc.anchor=GridBagConstraints.CENTER;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridwidth=1;
		gbc.gridy=1;
		panel.add(langALabel,gbc);
		gbc.gridy++;
		gbc.anchor=GridBagConstraints.NORTHWEST;
		panel.add(errorContainerA,gbc);
		gbc.anchor=GridBagConstraints.CENTER;
		gbc.gridy++;
		panel.add(t1LP,gbc);
		gbc.gridy++;
		gbc.anchor=GridBagConstraints.NORTHWEST;
		panel.add(meaningsPanelA,gbc);
		gbc.gridy++;
		panel.add(editBtnPanelA,gbc);
		
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.anchor=GridBagConstraints.CENTER;
		panel.add(langBLabel,gbc);
		gbc.gridy++;
		gbc.anchor=GridBagConstraints.NORTHWEST;
		panel.add(errorContainerB,gbc);
		gbc.anchor=GridBagConstraints.CENTER;
		gbc.gridy++;
		panel.add(t2LP,gbc);
		gbc.gridy++;
		gbc.anchor=GridBagConstraints.NORTHWEST;
		panel.add(meaningsPanelB,gbc);
		gbc.gridy++;
		panel.add(editBtnPanelB,gbc);
		
		gbc.gridx=0;
		gbc.gridy++;
		gbc.gridwidth=2;
		gbc.fill = GridBagConstraints.NONE;
		panel.add(sectionPanel,gbc);
	}
	
	private void setTabbedPane(JTabbedPane pane, Vector<MeaningFormularPanel> meanings){
		pane.removeAll();
		int index = 1;
		for(MeaningFormularPanel p : meanings){
			pane.addTab(""+index++, p);
		}
	}
	
	public void setTermClassA(String arg){
		termclassACB.setSelectedItem(arg);
	}
	
	public String getTermClassA(){
		return (String)termclassACB.getSelectedItem();
	}
	
	public void setTermClassB(String arg){
		termclassBCB.setSelectedItem(arg);
	}
	public String getTermClassB(){
		return (String)termclassBCB.getSelectedItem();
	}
	
	public String getSection(){
		if(!stLectCB.isSelected()) return sectionChooser.getSectionSelection();
		else return amwPanel.getStandardSection();
	}
	
	public void resetAllInputs(){
		for(MeaningFormularPanel fp : meaningsA){
			fp.resetAllInputs();
		}
		for(MeaningFormularPanel fp : meaningsB){
			fp.resetAllInputs();
		}
		sectionChooser.resetInputs();
		if(!stLectCB.isSelected()) stLectCB.doClick();
	}
	
	public void setPanelsA(){
		for(MeaningFormularPanel mr : meaningsA){
			mr.setTermClass(vocToEdit.getTermclassA((String) termclassACB.getSelectedItem()),(String) termclassACB.getSelectedItem());
		}
	}

	public void setPanelsB(){
		for(MeaningFormularPanel mr : meaningsB){
			mr.setTermClass(vocToEdit.getTermclassB((String) termclassBCB.getSelectedItem()),(String) termclassBCB.getSelectedItem());
		}
	}
	
	public void resetErrors(){
		errorContainerA.removeAll();
		errorContainerB.removeAll();
		errorContainerAB.removeAll();
	}
	
	public void setErrorAB(String txt){
		errorContainerAB.removeAll();
		errorLabelAB.setText(txt);
		errorContainerAB.add(errorLabelAB);
	}
	
	public void setErrorA(String txt){
		errorLabelA.setText(txt);
		errorContainerA.add(errorPanelA);
	}
	
	public void setErrorB(String txt){
		errorLabelB.setText(txt);
		errorContainerB.add(errorPanelB);
	}
	
	public boolean isInaccurate(){
		if(!getSection().equals("")){
			if(!GuiTools.checkInput(getSection())){
				setErrorAB(LANG.getSection()+": "+LANG.getNotThoseSigns()+" ("+GuiTools.NOTALLOWEDSIGNS+")");
				return true;
			}
		}
		for(MeaningFormularPanel mr : meaningsA){
			if(!mr.checkInputs()){
				setErrorA(mr.getErrorMessage());
				return true;
			}
		}
		for(MeaningFormularPanel mr : meaningsB){
			if(!mr.checkInputs()){
				setErrorB(mr.getErrorMessage());
				return true;
			}
		}
		return false;
	}
	
	public Vector<Meaning> getMeaningsA(){
		Vector<Meaning> mngs = new Vector<Meaning>();
		for(MeaningFormularPanel mr : meaningsA){
			if(mr.checkInputs()) mngs.add(mr.getMeaning());
		}
		return mngs;
	}
	public Vector<Meaning> getMeaningsB(){
		Vector<Meaning> mngs = new Vector<Meaning>();
		for(MeaningFormularPanel mr : meaningsB){
			if(mr.checkInputs()) mngs.add(mr.getMeaning());
		}
		return mngs;
	}
	
	public void grabFocusFirstElement(){
		meaningsA.elementAt(0).grabFocusFirstTF();
	}
	
	private class AddMeaningListenerA implements TAction{
		public void perform(){
			meaningsA.add(new EWDFormularPanel(parentFrame,SET,vocToEdit.getTermclassA((String) termclassACB.getSelectedItem()),vocToEdit.getCategoriesA(),meaningsA.size()+1));
			setTabbedPane(meaningsPanelA,meaningsA);
			delMeaningBtnA.setEnabled(true);
		}
	}
	
	private class DelMeaningListenerA implements TAction{
		public void perform(){
			if(meaningsA.size()>1){
				meaningsA.removeElementAt(meaningsPanelA.getSelectedIndex());
				int i=1;
				for(MeaningFormularPanel p : meaningsA){
					p.setMeaningTFTitle(""+i++);
				}
				setTabbedPane(meaningsPanelA,meaningsA);
				delMeaningBtnA.setEnabled(meaningsA.size()>1);
			}
		}
	}
	
	private class AddMeaningListenerB implements TAction{
		public void perform(){
			meaningsB.add(new EWDFormularPanel(parentFrame,SET,vocToEdit.getTermclassB((String) termclassBCB.getSelectedItem()),vocToEdit.getCategoriesB(),meaningsB.size()+1));
			setTabbedPane(meaningsPanelB,meaningsB);
			delMeaningBtnB.setEnabled(true);
		}
	}
	
	private class DelMeaningListenerB implements TAction{
		public void perform(){
			if(meaningsB.size()>1){
				meaningsB.removeElementAt(meaningsPanelB.getSelectedIndex());
				int i=1;
				for(MeaningFormularPanel p : meaningsB){
					p.setMeaningTFTitle(""+i++);
				}
				setTabbedPane(meaningsPanelB,meaningsB);
				delMeaningBtnB.setEnabled(meaningsB.size()>1);
			}
		}
	}
}